/*
 * shmwriter.cpp
 *
 *  Created on: Jul 14, 2013
 *      Author: m
 */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "NewSharedMemory.h"

using namespace ipc;

const int shmSize = 2;
const int dataPosition = 0;
const int writeCounterPosition = 1;

int main ( void )
{
    char mostrar[10240];
    int data;
    int writeCounter;

    SharedMemory<int> shm = SharedMemory<int>::get( "/tmp/myshm", 'a' );

    sprintf( mostrar,
            "SHMWRT: "
            "Ingrese valor a escribir en la shmem (>= 0)\n" );
    write( fileno(stdout), mostrar, strlen(mostrar) );

    while ( std::cin >> data )
    {

        shm.write( data, dataPosition );

        // Luego incremento en 1 el contador de escrituras
        writeCounter = shm.read( writeCounterPosition );
        writeCounter++;
        shm.write( writeCounter, writeCounterPosition );

        if ( data < 0 )
        {
            break;
        }
    }

    return 0;
}
